﻿using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {


	public PlayerControl player;
	public LevelManager levelManager;
	public StartMenu startMenu;
	public FinishMenu finishMenu;

	public Font GUIFont;

	public LayerMask GUILayerMask;
	public static GameManager Game;

	private bool isPaused = true;
	private bool isGameOver = false;
	private int lastScore = 0;
	private int bestScore = 0;

	/// <summary>
	/// Gets the last score.
	/// </summary>
	/// <value>The last score.</value>
	public int LastScore
	{
		get { return lastScore; }
	}
	/// <summary>
	/// Gets the best score.
	/// </summary>
	/// <value>The best score.</value>
	public int BestScore
	{
		get { return bestScore; }
	}
	/// <summary>
	/// Gets the in progress score.
	/// </summary>
	/// <value>The in progress score.</value>
	public int InProgressScore
	{
		get { return (int)levelManager.TotalDistance; }
	}
	/// <summary>
	/// Gets or sets a value indicating whether this instance is paused.
	/// </summary>
	/// <value><c>true</c> if this instance is paused; otherwise, <c>false</c>.</value>
	public bool IsPaused
	{
		get { return isPaused; }
		set
		{
			isPaused = value;
			Time.timeScale = isPaused ? 0.0f : 1.0f;
		}

	}
	/// <summary>
	/// Gets a value indicating whether this instance is game over.
	/// </summary>
	/// <value><c>true</c> if this instance is game over; otherwise, <c>false</c>.</value>
	public bool IsGameOver
	{
		get { return isGameOver; }
	}
	/// <summary>
	/// Awake this instance.
	/// </summary>
	void Awake()
	{
		if (Game != null) 
		{
			Debug.LogError ("More than one GameManager found in the scene");
			return;
		}

		Game = this;
		GUIFont.material.mainTexture.filterMode = FilterMode.Point;
		bestScore = PlayerPrefs.GetInt ("BestScore", 0);
	}

	public void Restart()
	{
		levelManager.Restart ();
		player.Restart ();
		IsPaused = false;
		isGameOver = false;

		SoundManager.PlayMusic ("GameLoop");
	}
	/// <summary>
	/// Raises the player death event.
	/// </summary>
	public void OnPlayerDeath()
	{
		lastScore = (int)levelManager.TotalDistance;
		if (lastScore > bestScore) {
			bestScore = lastScore;
			PlayerPrefs.SetInt ("BestScore", bestScore);
		}

		isGameOver = true;
		finishMenu.Activate ();
	}

	public void ShowTitleScreen()
	{
		levelManager.Restart ();
		IsPaused = true;

		startMenu.Activate ();
	}
	/// <summary>
	/// Updates the input.
	/// </summary>
	private void UpdateInput()
	{
		bool tapped = false;
		Vector3 tapPos = Vector3.zero;

		if (Input.GetMouseButtonDown (0))
		{
			tapped = true;
			tapPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
		}

		Touch touch;
		for (int i = 0; i < Input.touches.Length; i++)
		{
			touch = Input.touches [i];
			if (touch.phase == TouchPhase.Began) 
			{
				tapped = true;
				tapPos = Camera.main.ScreenToWorldPoint (touch.position);
			}
		}

		if (tapped) 
		{
			RaycastHit2D hit = Physics2D.Raycast (tapPos, Vector2.zero, 1.0f, GUILayerMask);
			if (hit.collider != null) 
			{
				MenuItem menuItem = hit.collider.gameObject.GetComponent<MenuItem> ();
				if (menuItem != null) 
				{
					if (menuItem.sfxName != "")
						SoundManager.PlaySfx (menuItem.sfxName);
					
					menuItem.SendMessageUpwards(menuItem.handler);
				}
			}
		}
	}
	/// <summary>
	/// Update this instance.
	/// </summary>
	void Update()
	{
		UpdateInput ();
	}
}
